/*
 *
 *日历base类
 *
 */
(function(win){
    var doc = win.document;
    var isFunction = function(obj){
        return Object.prototype.toString.call(obj) === "[object Function]"
    };
    var week = {
        zh: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
    }, monthMaxDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], monthMaxDaysLeap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    var Calendar = function(){
        this.dayNow = new Date();
        
        
    }
    Calendar.prototype = {
        constructor: Calendar,
        init: function(beforeFirstDay, processCurrentDay, processNormalDay, processResetDay){
            var that = this;
            this.processBeforeFirst = beforeFirstDay ||
            function(){
            };
            this.processCurrentDay = processCurrentDay ||
            function(){
            };
            this.processNormalDay = processNormalDay ||
            function(){
            };
            this.processResetDay = processResetDay ||
            function(){
            }
            return that;
        },
        generateDate: function(year, month, day, column){
            // current Date
            column = column || 1;
            var that = this;
            var curDay = that.dayNow.getDate();
            var curMonth = that.dayNow.getMonth();
            var curYear = that.dayNow.getFullYear();
            
            // if a date already exists, we calculate some values here
            if (!year) {
                var year = curYear;
                var month = curMonth;
            }
            
            // first day of the month.
            var fristDayOfMonthObj = new Date(year, month, 1);
            var firstDayOfMonth = fristDayOfMonthObj.getDay();
            var d = 0, count = 0, rowCount = 0, preMonthMax = that.getDaysPerMonth(month, year), firstRow = true, lastDay = 0;
            while (d <= preMonthMax) {
                //补足本月第一天之前的天数
                if (firstRow) {
                    if (isFunction(that.processBeforeFirst)) {
                        //lastDay = that.processPreviewMonth(year, month);
                        if (firstDayOfMonth > 0) {
                            while (count < firstDayOfMonth) {
                                count++;
                                that.processBeforeFirst.call(that, count, year, month + 1, d, week.zh[count]);
                                //firstDayOfMonth--;
                            }
                        }
                    }
                    firstRow = false;
                    var d = 1;
                    count = firstDayOfMonth ? firstDayOfMonth : 7;
                }
                
                firstDayOfMonth = count % (7 * column);
                if (firstDayOfMonth == 0) {//行到头了 处理下一行了
                    rowCount++;
                }                
                if (curDay == d && curMonth == month && curYear == year) {
                    //设置当前样式
                    //that.appendDate('<div class="' + config.css.today + '" ><span>' + d + '</span></div>', d);
                    that.processCurrentDay.call(that, count, year, month + 1, d, week.zh[firstDayOfMonth]);
                }
                else {
                    //处理普通样式
                    that.processNormalDay.call(that, count, year, month + 1, d, week.zh[firstDayOfMonth]);
                    //that.appendDate('<div><span>' + d + '</span></div>', d);
                }
                //增加数据
                count++;
                d++;
            }
            var reset = Math.ceil(count / (7 * column)) * 7 * column - preMonthMax - firstDayOfMonth;
            
            that.processResetDay.call(that, reset);
            
            return that.day;
        },
        getDaysPerMonth: function(month, year){
            //得到当月的天数
            if ((year % 4) == 0) {
                if ((year % 100) == 0 && (year % 400) != 0) {
                    return monthMaxDays[month];
                }
                return monthMaxDaysLeap[month];
            }
            else {
                return monthMaxDays[month];
            }
            
        }
        
    }
    
    win.Calendar = Calendar;
})(window)
